<?php
session_start();
//包含头
include '../PHP/head.php';
?>
<?php
//包含bootstrap的顶端按钮
include '../PHP/menu.php';
?>

<br />
<br />
<p class="text-center lead">澄清发出</p>
<?php
//接收操作
if (isset($_GET['sent'])) {
	$clarification_id = $_GET['clarification_id'];
	require_once ('../FNS/db_fns.php');
		
		$db = db_connect();
		
		$query="update clarification set status = '发出' where id = '".$clarification_id."'";
		$result=$db->query ($query);
		if ($result){
			echo "<br />";
			echo "<p>发出设置成功。</p>";
		}else{
			echo "<br />";
			echo"<p>发出设置失败！</p>";
	
		$db->close();
		}
	
}

if (isset($_GET['back'])) {
	//填写客户澄清回复
	$clarification_id = $_GET['clarification_id'];
	$project_id = $_GET['project_id'];
	
	echo "
	<form class='form-horizontal' action = '../PHP/show_clarification_checked.php' method = 'get'>
		<input type='hidden' name = 'project_id' value = '".$project_id."'>
		<input type='hidden' name = 'clarification_id' value = '".$clarification_id."'>
		<div class='form-group'>
			<label for='client_resp' class='col-md-2 control-label'>客户回复：</label>
			<div class='col-md-10'>
				<textarea class='form-control' rows='2' name='client_resp'></textarea>
			</div>
		</div>
		<br />
		<div class='form-group'>
			<div class='col-md-offset-2 col-md-3'>
				<input class='btn btn-success' type = 'submit' value='填入回复'/>
			</div>
		</div>
	</form>
	";
}

if (isset($_GET['client_resp'])) {
	//客户回复写入数据库
	$clarification_id = $_GET['clarification_id'];
	$client_resp = $_GET['client_resp'];
	
	//数据过滤和处理
	//加引号避免输入数据库出错
	if (! get_magic_quotes_gpc()) {	
	$clarification_id = addslashes ($clarification_id);
	$client_resp = addslashes ($client_resp);
	}
	
	$resp_date = date('Y-m-d');
	
	require_once ('../FNS/db_fns.php');
		
	$db = db_connect();
		
	$query="update clarification set status = '回复', client_resp = '".$client_resp."', resp_date = '".$resp_date."' where id = '".$clarification_id."'";
	
	$result=$db->query ($query);
		if ($result){
			echo "<br />";
			echo "<p>填写成功。</p>";
		}else{
			echo "<br />";
			echo"<p>填写失败！</p>";
	
		}
		//专业状态改回"1" - 进行中。
		$query="select project_id, subject from clarification where id = '".$clarification_id."'";
		$result=$db->query ($query);
		$row=$result->fetch_assoc ();
		$project_id = $row['project_id'];
		$subject = stripslashes ($row['subject']);
		
		$result->free();
		
		$query="update projects set ".$subject."_flag = '1' where id = '".$project_id."'";
		$result=$db->query ($query);
		
		$db->close();
	
}



if (isset($_GET['project_id'])) {
//接收GET过来的project_id，从clarification表里搜出project_id的且status是'校核'的澄清
require_once ('../FNS/db_fns.php');

$project_id = $_GET['project_id'];

$db = db_connect();
$query="select projects.code as project_code, projects.name as project_name, projects.sale as sale_id, projects.entrust_date, users.name as sale_name from projects inner join users on projects.sale = users.id where projects.id = '".$project_id."'";
	
$result=$db->query ($query);
$num_result=$result->num_rows;
	
//拿出数据
$row=$result->fetch_assoc ();
$project_code = stripslashes ($row['project_code']);
$project_name = stripslashes ($row['project_name']);
$sale_name = stripslashes ($row['sale_name']);
$sale_id = stripslashes ($row['sale_id']);

	
$result->free();
	
//列出编号基本信息
echo "
	<br />
	<div class='row'>
		<div class='col-md-2'><p class='text-right'>报价编号：</p></div>
		<div class='col-md-2'><p>".$project_code."</p></div>
		<div class='col-md-2'><p class='text-right'>项目名称：</p></div>
		<div class='col-md-4'><p>".$project_name."</p></div>
		<div class='col-md-1'><p class='text-right'>销售：</p></div>
		<div class='col-md-1'><p>".$sale_name."</p></div>
	</div>
	<br />
";

$query="select clarification.id, clarification.ref, clarification.des, clarification.status, clarification.client_resp, users.name as issue_name from clarification 
	inner join users on clarification.user_id = users.id 
	where clarification.project_id = '".$project_id."' order by clarification.status";
$result=$db->query ($query);
if (!$result) {
	echo "<br />";
	echo "<p>没有需要处理的澄清。</p>";
	echo "</div>";
	echo "</body>";
	$db->close();
exit;
}
$num_result=$result->num_rows;
//写表头
echo "<table class='table table-striped'>";
echo "<tr>";
	echo "<td align='center' class='info'><strong>客户文件参考</strong></td>";
	echo "<td align='center' class='info'><strong>澄清内容</strong></td>";
	echo "<td align='center' class='info'><strong>客户回复</strong></td>";
	echo "<td align='center' class='info'><strong>提出人</strong></td>";
	echo "<td align='center' class='info'><strong>状态</strong></td>";
	echo "<td align='center' class='info'><strong>操作</strong></td>";
echo "</tr>";



//循环编制表格
for ($i=0; $i < $num_result; $i++) {

	$row=$result->fetch_assoc ();
	
	$ref = stripslashes ($row['ref']);
	$des = stripslashes ($row['des']);
	$client_resp = stripslashes ($row['client_resp']);
	$issue_name = stripslashes ($row['issue_name']);
	$status = stripslashes ($row['status']);
	$clarification_id = stripslashes ($row['id']);
	
	$link_sent = "../PHP/show_clarification_checked.php?clarification_id=".$clarification_id."&sent=1&project_id=".$project_id;
	$link_back = "../PHP/show_clarification_checked.php?clarification_id=".$clarification_id."&back=1&project_id=".$project_id;
	
	echo "<tr>";
	echo "<td align ='center'>".$ref."</td>";
	echo "<td align ='center'>".$des."</td>";
	echo "<td align ='center'>".$client_resp."</td>";
	echo "<td align ='center'>".$issue_name."</td>";
	echo "<td align ='center'>".$status."</td>";
	echo "<td align='center'>";
		if ($status == '校核') {echo "<a class='btn btn-success btn-sm' href='".$link_sent."' role='button'>已发出</a>";}
		if ($status == '发出') {echo "<a class='btn btn-info btn-sm' href='".$link_back."' role='button'>填回复</a>";}
	echo "</td>";
	echo "</tr>";
	
}
echo "</table>";


$result->free();
$db->close();
}
?>




</div>
</body>
</html>